//
//  CXYMyInfoHeadView.swift
//  诚叙缘
//
//  Created by sujeking on 17/3/29.
//  Copyright © 2017年 szw. All rights reserved.
//

import UIKit

class CXYMyInfoHeadView: UIView {
    var path : CAShapeLayer!
    var path2 : CAShapeLayer!
    var bgImageView : UIImageView!
    
    //提供给外部使用的block
    typealias completeCallBack = () -> Void
    //居然是个类型  这里要注意
    var callBack : completeCallBack?
    
    
    
    override init(frame: CGRect) {
        super.init(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 270))
        setupSubViews()
        setupLayers()
    }
    
    required init(coder aDecoder: NSCoder)
    {
        super.init(coder: aDecoder)!
        setupLayers()
    }
    
    func setupSubViews() {
        self.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        
        bgImageView = UIImageView(frame: self.bounds)
        bgImageView.image = #imageLiteral(resourceName: "Bitmap")
        bgImageView.contentMode = UIViewContentMode.top
        bgImageView.clipsToBounds = true
        bgImageView.alpha = 0.2;
        bgImageView.center = CGPoint(x: self.center.x, y: self.center.y - 10)
        self.addSubview(bgImageView)
    }
    
    func setupLayers(){
        path = CAShapeLayer()
        path.frame       = CGRect(x: 0, y: 0, width: self.bounds.width - 30, height: 160)
        path.position      = CGPoint(x : self.center.x,y : self.bounds.height - 80)
        path.fillColor   = nil
        path.strokeColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1).cgColor
        path.lineWidth   = 1
        path.path        = pathPath().cgPath;
        
        
        path2 = CAShapeLayer()
        path2.frame       = CGRect(x: 0, y: 0, width: self.bounds.width - 30, height: 160)
        path2.position      = CGPoint(x : self.center.x,y : self.bounds.height - 80)
        path2.fillColor   = nil
        path2.strokeColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 0.4).cgColor
        path2.lineWidth   = 1
        path2.path        = untitledPath().cgPath;
        path2.strokeEnd   = 0;
        
        self.layer.addSublayer(path2)
        self.layer.addSublayer(path)
    }
    
    open func startAllAnimations() {
        path.add(pathAnimation(), forKey: "pathAnimation")
        path2.add(path2Animation(), forKey: "path2Animation")
    }
    
    func pathAnimation() -> CAAnimationGroup{
        let strokeEndAnim       = CABasicAnimation(keyPath:"strokeEnd")
        strokeEndAnim.fromValue = 0;
        strokeEndAnim.toValue   = 1;
        strokeEndAnim.duration  = 1
        
        let fillColorAnim       = CABasicAnimation(keyPath:"fillColor")
        fillColorAnim.toValue   = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1).cgColor;
        fillColorAnim.duration  = 1
        fillColorAnim.beginTime = 1
        
        let pathAnimGroup                 = CAAnimationGroup()
        pathAnimGroup.animations          = [strokeEndAnim,fillColorAnim]
        pathAnimGroup.fillMode            = kCAFillModeForwards
        pathAnimGroup.isRemovedOnCompletion = false
        pathAnimGroup.duration = 2
        return pathAnimGroup;
    }
    
    
    func path2Animation() -> CAAnimationGroup{
        
        let strokeEndAnim       = CABasicAnimation(keyPath:"strokeEnd")
        strokeEndAnim.fromValue = 0
        strokeEndAnim.toValue   = 1
        strokeEndAnim.duration  = 1
        strokeEndAnim.beginTime = 2
        
        let fillColorAnim       = CABasicAnimation(keyPath:"fillColor")
        fillColorAnim.toValue   = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 0.4).cgColor;
        fillColorAnim.duration  = 1
        fillColorAnim.beginTime = 3
        
        let pathAnimGroup                 = CAAnimationGroup()
        pathAnimGroup.animations          = [strokeEndAnim, fillColorAnim]
        pathAnimGroup.fillMode            = kCAFillModeForwards
        pathAnimGroup.isRemovedOnCompletion = false
        pathAnimGroup.duration = 4
        pathAnimGroup.delegate = self
        
        return pathAnimGroup;
    }
    
    //MARK: - Bezier Path
    
    func pathPath() -> UIBezierPath{
        let pathPath = UIBezierPath()
        pathPath.move(to: CGPoint(x: 0, y: 160))
        pathPath.addLine(to: CGPoint(x: path.bounds.width, y: 0))
        pathPath.addLine(to: CGPoint(x: path.bounds.width, y: 160))
        pathPath.addLine(to: CGPoint(x: 0, y: 160))
        
        return pathPath;
    }
    
    func untitledPath() -> UIBezierPath{
        let untitledPath = UIBezierPath()
        untitledPath.move(to: CGPoint(x: self.center.x, y: 100))
        untitledPath.addLine(to: CGPoint(x : 0, y : 0 ))
        untitledPath.addLine(to: CGPoint(x : 0, y : 160))
        untitledPath.close()
        untitledPath.move(to: CGPoint(x : self.center.x, y : 80))
        
        return untitledPath;
    }
}

extension CXYMyInfoHeadView : CAAnimationDelegate {
    func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
        animationForbgImageView()
    }
    
    func animationForbgImageView() {
        UIView.animate(withDuration: 3, animations: {
            self.bgImageView.alpha = 0.5
            self.bgImageView.center = self.center
            
        }) { (flag) in
            if(flag) {
                self.addIcons()
            }
        }
    }
    
    func addIcons() {
        let iconsImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 16, height: 90))
        iconsImageView.center = CGPoint(x: self.bounds.width - 8 - 15, y: self.bounds.height - 64)
        iconsImageView.image = #imageLiteral(resourceName: "icons")
        iconsImageView.alpha = 0;
        self.addSubview(iconsImageView)
        showIconsAnimation(iconView: iconsImageView)
        
        for i in 0...3 {
            let label = UILabel(frame: CGRect(x: 0, y: 0, width: 120, height: 15))
            label.tag = i
            label.font = UIFont.systemFont(ofSize: 12)
            switch i {
            case 0:
                label.text = "sujeking"
                break
            case 1:
                label.text = "15289714668"
                break
            case 2:
                label.text = "香港九龙半岛 湘西路1999号"
                label.adjustsFontSizeToFitWidth = true
                break
            case 3:
                label.text = "jeking"
                break
            default: break
                
            }
            
            label.textAlignment = NSTextAlignment.right
            label.textColor = #colorLiteral(red: 0.2549019754, green: 0.2745098174, blue: 0.3019607961, alpha: 1)
            label.alpha = 0
            label.center = CGPoint(x: self.bounds.width - 100, y: self.bounds.height - 25 - (CGFloat)(i * 25))
            self.addSubview(label)
            UIView.animate(withDuration: 0.2, delay: 0.3, options: UIViewAnimationOptions.curveLinear, animations: {
                label.alpha = 1
            }, completion: { (flag) in
                if flag {
                    if (self.callBack != nil){
                        self.callBack!()
                    }
                }
            })
        }
    }
    
    func showIconsAnimation(iconView:UIImageView?) {
        UIView.animate(withDuration: 0.3, animations: {
            iconView?.alpha = 1
        })
    }
}
